package company.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Activities;
import company.domain.Role;
import company.domain.Straypets;
import company.mapper.RoleMapper;
import company.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liuzewei
 * @ description
 * @ date 2024/7/24
 */
@RestController
public class RoleController {
    @Resource
    RoleService roleService;

    /*
     * 分页查询所有角色
     * */
    @RequestMapping("/selectAllRole/{page}")
    public R selectAllRole(@PathVariable("page") int page){
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,2);
        List<Role> list=this.roleService.list();
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
     * 根据id查询角色信息
     * */
    @PostMapping("/upDaRole")
    public R upDaRole(@RequestBody Role role){
        Role byId = this.roleService.getById(role.getId());
        return new R().add("byId",byId);
    }


    /*
     * 新增角色
     * */
    @PostMapping("/AddRole")
    public R AddRole(@RequestBody Role role){
        boolean save = this.roleService.save(role);
        if (save){
            new R().setCode(502);
        }
        return new R();
    }


    /*
     * 流浪宠物信息删除
     * */
    @RequestMapping("/delRole/{id}")
    public R delRole(@PathVariable("id") int id){
        boolean remove = this.roleService.removeById(id);
        R r=new R();
        if(!remove){
            r.setCode(502);
        }
        return r;
    }
}
